<template>
  <div>
    <!-- 面包屑 -->
    <el-bread></el-bread>

    <!-- 添加按钮 -->
    <el-button
      type="primary"
      class="addBtn"
      size="small"
      @click="addDialog"
      plain
      >添加</el-button
    >

    <!-- 菜单管理主体部分 -->
    <el-table
      :data="cateList"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="分类编号"> </el-table-column>

      <el-table-column prop="catename" label="分类名称"> </el-table-column>

      <el-table-column prop="img" label="图片">
        <template v-slot="{ row }">
          <img :src="row.img" v-if="row.img" width="150px" height="100px" />
        </template>
      </el-table-column>

      <el-table-column prop="status" label="菜单状态">
        <template v-slot="$prop">
          <el-button
            v-if="$prop.row.status == 1"
            type="success"
            disabled
            size="small"
            round
            >正常</el-button
          >
          <el-button v-else type="warning" size="small" disabled round
            >禁用</el-button
          >
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template v-slot="$prop">
          <el-button
            type="primary"
            @click="edit($prop.row.id)"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="del($prop.row.id)"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗 -->
    <v-dialog ref="diaLog"></v-dialog>
  </div>
</template>


<script>
//引入辅助性函数
import { mapState, mapActions } from "vuex";
//引入Dialog弹窗
import vDialog from "./dialog.vue";
//引入删除接口
import { delCate } from "../../request/api";
export default {
  //计算属性 使用辅助性函数mapState
  computed: {
    ...mapState("cate", ["cateList"]),
  },

  //方法 使用辅助性函数 mapActions
  methods: {
    ...mapActions("cate", ["getCate"]),

    //点击添加按钮
    addDialog() {
      this.$store.commit("cate/changeDialog", true);
    },

    //点击编辑事件
    edit(id) {
      //打开Dialog弹窗
      this.$store.commit("cate/changeDialog", true);
      //通过refs 让子组件里面的方法可以调用
      this.$refs.diaLog.dataForm(id);
    },

    //点击删除事件
    del(id) {
      //这是封装的全局删除方法
      this.$del(() => {
        delCate({ id }).then((res) => {
          if (res.code == 200) {
            //弹窗提示成功
            this.$message.success(res.msg);
            //重新获取数据
            this.getCate();
          }else{
            this.$message.error(res.msg);
          }
        });
      });
    },
  },

  //挂载完成
  mounted() {
    this.getCate();
  },

  //注册组件
  components: {
    vDialog,
  },
};
</script>


<style lang='less' scoped>
//添加按钮的样式
.addBtn {
  margin: 15px 5px;
}
</style>